package com.qf.commons.sentinel.config;

import com.qf.commons.sentinel.config.exception.GatewaySentinelExceptionHandler;
import com.qf.commons.sentinel.config.exception.IntervelSentinelExceptionHandler;
import com.qf.commons.sentinel.config.exception.ServiceSentinelExceptionHandler;
import com.qf.commons.standar.exception.IExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 自动装配的类
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@Configuration
@EnableConfigurationProperties(SentinelNacosConfig.class)
@Slf4j
public class SentinelAutoConfiguration {

    @Bean
    public ApplicationRunning getRunning(){
        return new ApplicationRunning();
    }



    @Configuration
    @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE)
    public static class GatewaySentinnel{
        /**
         * 网关层的Sentinel异常处理器
         * @return
         */
        @Bean
        public GatewaySentinelExceptionHandler getGatewayExceptionHandler(){
            log.info("[sentinel exception handler] - 网关层的Sentinel异常处理器被加载....");
            return new GatewaySentinelExceptionHandler();
        }
    }

    @Configuration
    @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
    public static class ServiceSentinel{
        /**
         * 普通微服务的Sentinel的异常处理器
         * @return
         */
        @Bean
        public ServiceSentinelExceptionHandler getServiceSentinelExceptionHandler(){
            log.info("[sentinel exception handler] - 微服务的Sentinel异常处理器被加载....");
            return new ServiceSentinelExceptionHandler();
        }

        /**
         * 内部的降级异常处理器
         * @return
         */
        @Bean
        public IExceptionHandler getIExceptionHandler(){
            return new IntervelSentinelExceptionHandler();
        }
    }
}
